/*
 * NonMotorizedTripItem.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.trip;

import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.fpdev.core.FPEngine;
import org.fpdev.core.basenet.Path;
import org.fpdev.core.basenet.BaseNetwork;
import org.fpdev.core.trip.NonMotorizedSubtrip;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;

public abstract class NonMotorizedTripItem implements TripItem {

  protected List<NonMotorizedSubtrip> subtrips_;
  private int lenSec_; // length to traverse path in seconds

  public NonMotorizedTripItem() {
    subtrips_ = new LinkedList<NonMotorizedSubtrip>();
  }

  public abstract String getItemName();

  public abstract void newSubtrip(Path path, int start, int end);

  public String getFrom(BaseNetwork net) {
    if (subtrips_.size() == 0) {
      return "err";
    }
    return subtrips_.get(0).getFrom(net);
  }

  public String getTo(BaseNetwork net) {
    if (subtrips_.size() == 0) {
      return "err";
    }
    return subtrips_.get(subtrips_.size() - 1).getTo(net);
  }

  public int getStart() {
    return subtrips_.get(0).getStart();
  }

  public int getEnd() {
    return subtrips_.get(subtrips_.size() - 1).getEnd();
  }

  public int getLenSec() {
    return lenSec_;
  }

  public void setLenSec(int lenSec) {
    lenSec_ = lenSec;
  }

  public List<Integer> getLinkIDList() {
    List<Integer> list = new LinkedList<Integer>();
    for(NonMotorizedSubtrip subtrip : subtrips()) {
      list.addAll(subtrip.getLinkIDList());
    }
    return list;
  }

  public String getXml(BaseNetwork net) {
    String xml = "";
    xml += "    <" + getItemName() + " from=\"" + getFrom(net) + "\" to=\"" + getTo(net) + "\" fnode=\"" + subtrips_.get(0).getPath().startNode().getID() + "\">\n";

    for(NonMotorizedSubtrip subtrip : subtrips()) {
      xml += subtrip.getXml(net);
    }

    xml += "    </" + getItemName() + ">\n";
    return xml;
  }

  public String getText() {
    return "";
  }

  public Path getPath(FPEngine engine) {
    try {
      Path path = new Path(engine);
      int si = 0;
      for(NonMotorizedSubtrip subtrip : subtrips()) {
        Path subPath = subtrip.getPath();
        path.append(subPath);
        si++;
      }
      return path;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public int subItemCount() {
    return subtrips_.size();
  }

  public Collection<NonMotorizedSubtrip> subtrips() {
    return subtrips_;
  }

  public Collection<NonMotorizedSubtrip> subtripsCopy() {
    return new LinkedList(subtrips_);
  }

  public void draw(MapCanvas canvas) {
    canvas.setLineWidth(2);
    for(NonMotorizedSubtrip subtrip : subtrips()) {
      subtrip.draw(canvas);
    }
  }

  public Rectangle2D getBoundingBox() {
    Rectangle2D box = null;
    for(NonMotorizedSubtrip subtrip : subtrips()) {
      Rectangle2D sbox = subtrip.getBoundingBox();
      if (sbox != null) {
        if (box == null) {
          box = sbox;
        } else {
          box = box.createUnion(sbox);
        }
      }
    }
    return box;
  }

  public int getMinElev() {
    int min = Integer.MAX_VALUE;
    for(NonMotorizedSubtrip subtrip : subtrips()) {
      min = Math.min(min, subtrip.getMinElev());
    }
    return min;
  }

  public int getMaxElev() {
    int max = Integer.MIN_VALUE;
    for(NonMotorizedSubtrip subtrip : subtrips()) {
      max = Math.max(max, subtrip.getMaxElev());
    }
    return max;
  }
}























